//
//  WBTitleButton.swift
//  仿写微博
//
//  Created by tuimao on 17/7/21.
//  Copyright © 2017年 tuimao. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {
    //重载并非重写
    init(title:String?) {
      super.init(frame: CGRect())
        //title  nil 情况
        if title == nil {
            setTitle("首页", for: [])
            
        }else{
            setTitle(title! + " ", for: [])
            // 设置图像
            setImage(UIImage(named: "navigationbar_arrow_down"), for: [])
            setImage(UIImage(named: "navigationbar_arrow_up"), for: .selected)
        }
        //设置标题颜色和字体大小
        setTitleColor(UIColor.darkGray, for: [])
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //重写子类的布局
    override func layoutSubviews() {
        super.layoutSubviews()
        guard let titleLabel = titleLabel,let imageView = imageView else{
            
            return
        }
        // Swift 中可以直接修改
        titleLabel.frame.origin.x = 0
        
        // 将 imageView 的 x 向右移动 label 的宽度
        imageView.frame.origin.x = titleLabel.bounds.width
    }
    
}

